#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Maxn = 110;
int n, m, k, ans = 1e18;
int a[Maxn], cnt[Maxn], b[Maxn][Maxn];
void dfs(int x){
	if(x > n){
		int sum = 0;
		for(int i = 1 ; i <= k ; i++){
			sort(b[i] + 1, b[i] + m + 1);
			for(int j = 2 ; j <= m ; j++)
				if(b[i][j] == b[i][j - 1]) return ;
			sum += b[i][m] - b[i][1];
		}
		ans = min(ans, sum);
		return ;
	}
	for(int i = 1 ; i <= k ; i++){
		if(cnt[i] >= m) continue;
		b[i][++cnt[i]] = a[x];
		dfs(x + 1);
		cnt[i]--;
	}
}
signed main(){
	ios::sync_with_stdio(false);
	freopen("diyiti.in", "r", stdin);
	freopen("diyiti.out", "w", stdout);
	cin >> n >> k;
	m = n / k;
	for(int i = 1 ; i <= n ; i++) cin >> a[i];
	dfs(1);
	if(ans == 1e18) cout << -1 << '\n';
	else cout << ans << '\n';
	return 0;
}